//
//  MYComposeBar.m
//  奇闻
//
//  Created by 似水灵修 on 14-12-1.
//  Copyright (c) 2014年 MingYueGuCheng. All rights reserved.
//

#import "MYComposeBar.h"

@implementation MYComposeBar
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"compose_toolbar_background"]];
        //添加所有子控件
        [self setupAllChildView];
    }
    return self;
}
 //添加所有子控件
- (void)setupAllChildView
{
    // 添加5个按钮,每个按钮的图标
    // 相册
    [self addOneChildViewWithNormalImage:[UIImage imageNamed:@"compose_toolbar_picture"] highImage:[UIImage imageNamed:@"compose_toolbar_picture_highlighted"]];
    // 提及
    [self addOneChildViewWithNormalImage:[UIImage imageNamed:@"compose_mentionbutton_background"] highImage:[UIImage imageNamed:@"compose_mentionbutton_background_highlighted"]];
    // 趋势
    [self addOneChildViewWithNormalImage:[UIImage imageNamed:@"compose_trendbutton_background"] highImage:[UIImage imageNamed:@"compose_trendbutton_background_highlighted"]];
    // 表情
    [self addOneChildViewWithNormalImage:[UIImage imageNamed:@"compose_emoticonbutton_background"] highImage:[UIImage imageNamed:@"compose_emoticonbutton_background_highlighted"]];
    // 键盘
    [self addOneChildViewWithNormalImage:[UIImage imageNamed:@"compose_keyboardbutton_background"] highImage:[UIImage imageNamed:@"compose_keyboardbutton_background_highlighted"]];
}
//添加一个按钮
- (void)addOneChildViewWithNormalImage:(UIImage *)normalImage highImage:(UIImage *)highImage
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:normalImage  forState:UIControlStateNormal];
    [btn setImage:highImage forState:UIControlStateHighlighted];
    btn.tag = self.subviews.count;
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btn];
}
#pragma mark - 底部button点击事件
- (void)btnClick:(UIButton *)btn
{
    if ([self.delegate respondsToSelector:@selector(composeBar:didClickBtn:)]) {
        [self.delegate composeBar:self didClickBtn:btn.tag];
    }
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    NSUInteger count = self.subviews.count;
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat w = self.width / count;
    CGFloat h = self.height;
    for (NSUInteger i = 0; i < count; i++) {
        x = i * w;
        UIButton *btn = self.subviews[i];
        btn.frame = CGRectMake(x, y, w, h);
    }
}
@end
